﻿using Sunny.UI;
using System;
using System.Drawing;
using System.Windows.Forms;
using WCS.Entity;
using WCS.Utils;
using static System.Collections.Specialized.BitVector32;

namespace WCS.Controls.系统查询
{
    public partial class PalletToMaterial : UIPage
    {
        public static PalletToMaterial _PalletToMaterial;
        public PalletToMaterial()
        {
            InitializeComponent();
            _PalletToMaterial = this;//AddMaterial
        }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper SqlSugarDapper = SqlSugarDapper.GetDbContext();

        private void AddTask_Load(object sender, EventArgs e)
        {
            Re();
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        public void Re()
        {
            this.uiDataGridView1.DataSource = SqlSugarDapper.DB.Queryable<Entity.PalletToMaterial>().ToList();
        }

        AddMaterial addMaterial = null;

        /// <summary>
        /// 跳转添加物料窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScoreAdd_Click_1(object sender, EventArgs e)
        {
            if (addMaterial == null || addMaterial.IsDisposed)
            {
                addMaterial = new AddMaterial();
                addMaterial.Show();
            }
            else
            {
                addMaterial.Show();
            }
        }

        /// <summary>
        /// 删除物料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.uiDataGridView1.CurrentRow == null || this.uiDataGridView1.RowCount == 0)
            {
                ShowWarningDialog("提示", "没有可删除的数据", UIStyle.Blue, false);
                return;
            }
            var code = uiDataGridView1.Rows[this.uiDataGridView1.SelectedIndex].Cells[0].Value.ToString();
            var codeName = uiDataGridView1.Rows[this.uiDataGridView1.SelectedIndex].Cells[1].Value.ToString();

            if (code == "" || codeName == "")
            {
                ShowWarningDialog("提示", "没有可删除的数据", UIStyle.Blue, false);
                return;
            }

            var count = SqlSugarDapper.DB.Queryable<WarehouseInfo>()
                .Where(w => w.barCode.Contains(code))
                .ToList()
                .Count;

            if (count != 0)
            {
                ShowWarningDialog("提示", "砂芯库还有此砂芯的库存无法删除", UIStyle.Blue, false);
                return;
            }

            DialogResult res = MessageBox.Show($"确定要删除{codeName}的物料信息吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (res == DialogResult.OK)
            {
                SqlSugarDapper.DB.Deleteable<Entity.PalletToMaterial>()
                .Where(w => w.PalletCode == code && w.MaterialType == codeName)
                .ExecuteCommand();

                MessageBox.Show($"删除铸造任务{codeName}成功！");

                Re();
            }
        }

        /// <summary>
        /// 右键删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    Point p = Control.MousePosition;
                    this.uiContextMenuStrip1.Show(p);

                    uiDataGridView1.ClearSelection();
                    uiDataGridView1.Rows[e.RowIndex].Selected = true;
                    uiDataGridView1.CurrentCell = uiDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
            }
        }
    }
}
